package com.dao;

import com.bean.Admin;
import com.common.DbConnect;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class AdminDao {
    public Admin getAdmin(String id){
        Connection cn = DbConnect.getConnection();
        PreparedStatement st = null;
        ResultSet rs = null;

        if (cn == null)
            return null;
        try {
            //4.执行sql
            String sql = "select * from administrator where account=?";
            st = cn.prepareStatement(sql);
            st.setString(1, id);

            rs = st.executeQuery();
            if (rs.next()) {//找到
                Admin admin =new Admin(rs.getString("account"),rs.getString("password"));
                return admin;
            } else {//未找到
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //5.关闭数据库连接
            DbConnect.close(cn, st, rs);
        }
        return null;
    }
}
